package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.dao.impl.BaseDataTemplate;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.util.List;

/**
 * 检查缺失的争议交易DAO
 *
 * @author MaShibo
 * 日期 :  2020/3/10
 */
@Repository
public class CheckMissingDisputedTransDao extends BaseDataTemplate {

    public Integer queryPortalTransOutState(String transactionId) {
        final String sql = "SELECT tr.state FROM tr_portal_transout tr WHERE tr.transactionid = ?";
        return jdbcTemplate.query(sql, rs -> {
            if (rs.next()) {
                return (Integer) JdbcUtils.getResultSetValue(rs, 1, Integer.class);
            }
            return null;
        }, transactionId);
    }


    public boolean isOutProvinceTrans(String transactionId) {
        final String sql =
                "select 1\n" +
                        "  from tr_swipe_split_out sw\n" +
                        " where sw.transactionid = ?\n" +
                        "   and sw.tollprovinceid <> '610201'";
        return jdbcTemplate.query(sql, ResultSet::next, transactionId);
    }


    public List<String> queryBOCDisputedTransId() {
        final String sql =
                "select ds.transactionid\n" +
                        "  from tr_dispute_transout ds\n" +
                        "  join tr_portal_transout tr\n" +
                        "    on ds.transactionid = tr.transactionid\n" +
                        " where ds.disputedid = 6100000106208871\n" +
                        "   and not exists (select 1\n" +
                        "          from tr_swipe_split_out sw\n" +
                        "         where sw.transactionid = ds.transactionid\n" +
                        "           and sw.tollprovinceid <> '610201')";
        return jdbcTemplate.queryForList(sql, String.class);
    }
}
